package com.film.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.service.UserService;
import com.film.service.impl.UserServiceImpl;

/**
 * 该控制类用来做注册
 * @author 2119196781
 *
 */
@WebServlet("/register")
public class RegisterController extends HttpServlet{
	
	private UserService userService = new UserServiceImpl();

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String nickName = request.getParameter("nickName");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String phoneNum = request.getParameter("phoneNum");
		String verify = request.getParameter("verify");
		HttpSession session = request.getSession();
		String verifyCode = (String)session.getAttribute("code");
		if(verifyCode.equalsIgnoreCase(verify)) {
			int result = userService.register(username, password, phoneNum, nickName);
			if(result == 1){
				//注册成功，跳转到登录页面
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else if(result == 0){
				response.sendRedirect("register.jsp?flag=2");//用户名重复
			}else if(result == 2) {
				response.sendRedirect("register.jsp?flag=3");//手机号重复
			}
		}else {
			response.sendRedirect("register.jsp?flag=1");//验证码错误
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
}
